﻿using BLL;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.其他窗体
{
    public partial class 销价填写 : Sunny.UI.UIForm
    {
        public string Danhao { get; set; } = string.Empty;
        private DanjuTable danju = new DanjuTable();
        private List<danjumingxitable> mingxis = new List<danjumingxitable>();
        public 销价填写()
        {
            InitializeComponent();
            CreateGrid.Create("销售发货单", gridView1);
            try
            {
                gridView1.Columns["IsHanshui"].ColumnEdit = cmbHanshui;
                comboBox1.Text = "含税";
                //gridView1.Columns["Kuwei"].ColumnEdit = LUEKuwei;

            }
            catch
            {
                Tools.ShowMessageBox.ShowErrorBox("创建表格的时候发送了错误!请参考销售发货单的配置列");
            }
            
        }

        private void 销价填写_Load(object sender, EventArgs e)
        {
            danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == this.Danhao);
            txtjine.Text = danju.je.ToString("N2");
            txtbaozhaungfei.Text = danju.PackagingFee.ToString("N2");
            txtchefei.Text = danju.yunfei.ToString("N2");
            txthuakunfei.Text = danju.FlowerRollerFee.ToString("N2");
            txtqitafeiyong.Text = danju.OtherFees.ToString("N2");
            txtzhuangxiefei.Text = danju.StevedoringChargesFree.ToString("N2");
            mingxis = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == this.Danhao).ToList ();
            gridControl1.DataSource = mingxis;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            mingxis.ForEach(x => x.IsHanshui = comboBox1.Text);
            gridControl1.RefreshDataSource();
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (gridView1.FocusedColumn.FieldName == "mashu")
            {
                mingxis[gridView1.FocusedRowHandle].toupimishu = mingxis[gridView1.FocusedRowHandle].mashu / 100 * mingxis[gridView1.FocusedRowHandle].chengpingmishu;
            }
            mingxis[gridView1.FocusedRowHandle].hanshuiheji = mingxis[gridView1.FocusedRowHandle].hanshuidanjia * mingxis[gridView1.FocusedRowHandle].toupimishu;
            txtjine.Text = mingxis.Sum(x => x.hanshuiheji).ToString("N2");
        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            try
            { 
                Connect.dbcontext.BeginTran();
                var olddanju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == this.Danhao);
                来往明细BLL.删除来往记录(olddanju);
                财务BLL.减少应收款(olddanju);
                财务BLL.减少应开发票(olddanju, Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == this.Danhao).ToList());
                danju.je = mingxis.Sum(x => x.hanshuiheji)+txtbaozhaungfei.Text.ObjToDecimal ()+txtzhuangxiefei.Text.ObjToDecimal ()+txtqitafeiyong.Text .ObjToDecimal ()+txtchefei .Text .ObjToDecimal ()+txthuakunfei.Text.ObjToDecimal ();
                danju.PackagingFee = txtzhuangxiefei.Text.ObjToDecimal();
                danju.yunfei = txtchefei.Text.ObjToDecimal();
                danju.StevedoringChargesFree = txtzhuangxiefei.Text.ObjToDecimal();
                danju.FlowerRollerFee = txthuakunfei.Text.ObjToDecimal();
                danju.OtherFees = txtqitafeiyong.Text.ObjToDecimal();
                Connect.dbcontext.Updateable(danju).ExecuteCommand();
                Connect.dbcontext.Updateable(mingxis).ExecuteCommand();
                来往明细BLL.增加来往记录(danju);
                财务BLL.增加应收款(danju);
                财务BLL.增加应开发票(mingxis, danju);
                Connect.dbcontext.CommitTran();
                this.Close();
            }
            catch(Exception ex)
            {
                Connect.dbcontext.RollbackTran();
                Tools.ShowMessageBox.ShowErrorBox("保存的时候发生了错误!"+ex.Message);
            }
           
        }
    }
}
